﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using VCDIFF.Rebuild;

namespace VCDIFF.Differencing.DiffAlgorithm
{
    public class TextFileAlgorithm : IVCDIFFAlgorithm
    {
        DiffEngineLevel Level;

        public TextFileAlgorithm(DiffEngineLevel level)
        {
            Level = level;
        }

        public VCDFile CreateVCDIFF(string filePath)
        {
            DiffList_TextFile original = new DiffList_TextFile(filePath);
            DiffList_TextFile result = new DiffList_TextFile(filePath + "_result");

            DiffEngine de = new DiffEngine();
            de.ProcessDiff(original, result, Level);
            ArrayList differences = de.DiffReport();

            ExecuteDiff.Execute(filePath, original, result, differences);

            byte[] data = File.ReadAllBytes(filePath + "_vcdiff");

            RebuildHeader rebuildHeader = new RebuildHeader();
            Header header = rebuildHeader.Rebuild(data);

            RebuildWindows rebuildWindows = new RebuildWindows();
            IList<Window> windowsList = rebuildWindows.Rebuild(data);

            VCDFile vcdFile = new VCDFile();
            vcdFile.Header = header;
            vcdFile.WindowsList = windowsList;

            return vcdFile;
        }
    }
}
